package com.qhs.sys.service.impl;

import com.qhs.framework.utils.DateUtil;
import com.qhs.framework.utils.IDWorker;
import com.qhs.framework.utils.SmsUtil;
import com.qhs.framework.utils.StringUtil;
import com.qhs.sys.mapper.SmsLogMapper;
import com.qhs.sys.entity.SmsLog;
import com.qhs.sys.exception.BusinessException;
import com.qhs.sys.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by xinchao on 2016/11/30 0030.
 */
@Service
public class SmsServiceImpl implements SmsService{

    @Autowired
    private SmsLogMapper smsLogMapper;
    @Autowired
    private IDWorker idWorker;
//    @Autowired
//    private RestTemplate restTemplate;

    /**
     * 通过阿里云平台发送短信
     *
     * @return
     */
    public String sendCloudMes(String recNum, String jsonParams){
        Map<String,Object> urlVariables = SmsUtil.assembleParam(recNum,jsonParams);
        String signature = SmsUtil.sign(urlVariables);
        urlVariables.put("Signature",signature);
       /* return restTemplate.postForEntity(SmsUtil.apiUrl+"?Action={Action}&SignName={SignName}&TemplateCode={TemplateCode}" +
                "&RecNum={RecNum}&ParamString={ParamString}&Format={Format}" +
                "&Version={Version}&Signature={Signature}&SignatureMethod={SignatureMethod}" +
                "&SignatureNonce={SignatureNonce}&SignatureVersion={SignatureVersion}&AccessKeyId={AccessKeyId}" +
                "&Timestamp={Timestamp}",null, String.class,urlVariables).getBody();*/
        return "";
    }

    @Override
    public int insertSms(SmsLog smsLog) throws BusinessException {
        if (smsLog.getId()==null){
            smsLog.setId(idWorker.generateId());
        }
        if (smsLog.getPhone()==null){
            throw new BusinessException("手机号码不能为空");
        }
        smsLog.setRequestTime(DateUtil.getDefaultDate());
        smsLog.setInvalidTime(DateUtil.getNextDate(10,"3"));
        //此处的验证码需要随机获取6位
        String verifyCode ="1111";
        //调用第三方短信接口封装类发送短信
        Map params = new HashMap();
        params.put("code",verifyCode);
        params.put("product","建科电子病历");
 //       sendCloudMes(smsLog.getPhone(), JsonUtil.toJson(params));
        smsLog.setVerifyCode(verifyCode);
        return smsLogMapper.insert(smsLog);
    }

    @Override
    public String verifySmsCode(String phone, String verifyCode, String codeType) throws BusinessException {
        //根据手机号和类型获取最近一次的短信记录
        SmsLog smsLog = new SmsLog();
        smsLog.setType(StringUtil.StringToInt(codeType));
        smsLog.setPhone(phone);
        smsLog= smsLogMapper.selectLastMes(smsLog);
        //检查是否超时
        if(verifyCode.equals(smsLog.getVerifyCode())){
            if(smsLog.getInvalidTime() < DateUtil.getDefaultDate()){
                throw new BusinessException("验证码超时，请重新获取");
            }
        }else{
            throw new BusinessException("验证码错误");
        }

        return "success";
    }
}
